{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ae081df6",
   "metadata": {},
   "source": [
    "# 7.1 LangChain基础模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c13be6ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='我喜欢编程。')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "os.environ[\"OPENAI_API_BASE\"]  = \"xxx\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"xxx\"\n",
    "chat = ChatOpenAI()\n",
    "messages = [\n",
    "    SystemMessage(content=\"You are a helpful assistant that translates English to Chinese.\"),\n",
    "    HumanMessage(content=\"I love programming.\"),\n",
    "]\n",
    "chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f3161240",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='Today is Sunday.\\nI am a boy.\\n', metadata={'source': './test.txt'})]\n"
     ]
    }
   ],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "\n",
    "loader = TextLoader(\"./test.txt\")\n",
    "print(loader.load())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1f411fdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 4 is greater than number of elements in index 3, updating n_results = 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狗\n"
     ]
    }
   ],
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_community.vectorstores import Chroma\n",
    "from langchain.docstore.document import Document\n",
    "import os\n",
    "os.environ[\"OPENAI_API_BASE\"]  = \"xxx\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"xxx\"\n",
    "\n",
    "raw_documents = [Document(page_content=\"葡萄\", metadata={\"source\": \"local\"}),\n",
    "Document(page_content=\"白菜\", metadata={\"source\": \"local\"}),\n",
    "Document(page_content=\"狗\", metadata={\"source\": \"local\"})]\n",
    "db = Chroma.from_documents(raw_documents, OpenAIEmbeddings())\n",
    "query = \"动物\"\n",
    "docs = db.similarity_search(query)\n",
    "print(docs[0].page_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c0f5a31",
   "metadata": {},
   "source": [
    "# 7.2.基于LangChain实现RAG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b3ec7e09",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.embeddings.openai.OpenAIEmbeddings` was deprecated in langchain-community 0.1.0 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import OpenAIEmbeddings`.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='\\n谷清水的昵称是战士金', metadata={'source': '7.2_RAG_demo.txt'}), Document(page_content='\\n谷清水是万能青年旅店、腰、华北浪革、生祥、声音碎片等乐队的粉丝', metadata={'source': '7.2_RAG_demo.txt'})]\n"
     ]
    }
   ],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain_community.vectorstores import Chroma\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.schema.runnable import RunnablePassthrough\n",
    "from langchain.schema.output_parser import StrOutputParser\n",
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_BASE\"]  = \"xxx\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"xxx\"\n",
    "\n",
    "loader = TextLoader('7.2_RAG_demo.txt',encoding = \"utf-8\")\n",
    "documents = loader.load()\n",
    "text_splitter = \\\n",
    "RecursiveCharacterTextSplitter(\n",
    "    chunk_size=10, chunk_overlap=0,separators=[\"\\n\"])\n",
    "chunks = text_splitter.split_documents(documents)\n",
    "template = \"\"\"你是一位问答助手,你的任务是根据###中间的文本信息回答问题，请准确回答问题，不要健谈，如果提供的文本信息无法回答问题，请直接回复“提供的文本无法回答问题”，我相信你能做的很好。###\\n{context}###\\n问题：{question}\"\"\"\n",
    "question = \"战士金喜欢哪写乐队？\"\n",
    "db = Chroma.from_documents(chunks, OpenAIEmbeddings())\n",
    "retriever = db.as_retriever(search_kwargs={\"k\": 2})\n",
    "context =  retriever.get_relevant_documents(question)\n",
    "print(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "164971ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='战士金喜欢万能青年旅店、腰、华北浪革、生祥、声音碎片等乐队。'\n"
     ]
    }
   ],
   "source": [
    "context_str = \"；\".join([d.page_content for d in context])\n",
    "input_str = template.format_map({\"context\":context_str,\"question\":question})\n",
    "chat = ChatOpenAI()\n",
    "messages = [SystemMessage(content=\"你是一位问答助手\"),HumanMessage(content=input_str)]\n",
    "response = chat(messages)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efc7cc2c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
